package com.oocl.euc.core.maintenance.controller;

import com.oocl.euc.core.maintenance.model.EmailTemplate;
import com.oocl.euc.core.maintenance.service.EmailTemplateService;
import com.oocl.euc.framework.base.controller.RESTController;
import com.oocl.euc.framework.base.service.BaseService;
import com.oocl.euc.framework.common.utils.ResponseEntityUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/rest/emailTemplate")
public class EmailTemplateController extends RESTController<EmailTemplate, Long> {

    @Autowired
    EmailTemplateService emailTemplateService;

    @Override
    protected BaseService<EmailTemplate, Long> getBaseService() {
        return emailTemplateService;
    }

    private Logger logger = LoggerFactory.getLogger(EmailTemplateController.class);

    @RequestMapping(value = "/findByEmailTempalteKey", method = RequestMethod.GET)
    public ResponseEntity findByEmailTempalteKey(@RequestParam String emailTemplateKey) {
        try {
            EmailTemplate emailTemplate = emailTemplateService.findByEmailTempalteKey(emailTemplateKey);
            if (emailTemplate == null) {
                return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
            }
            return new ResponseEntity<Object>(emailTemplate, HttpStatus.OK);
        } catch (Exception e) {
            logger.error("error", e);
            return new ResponseEntity<Object>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public ResponseEntity updateEmailTemplate(@RequestBody EmailTemplate emailTemplate) {
        try {
            emailTemplateService.update(emailTemplate);
            return ResponseEntityUtil.successInRestful(emailTemplate);
        } catch (Exception e) {
            logger.error("error", e);
            return new ResponseEntity<Object>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }

}
